#include <iostream>
#include <functional>
#include <unistd.h>
#include <cstring>
class Channel {
public:
    // 模拟的 Channel 类，包含读取缓冲区
    class ReadBuffer {
    public:
        int read(uint8_t* buffer, int size) {
            // 模拟读取数据
            strcpy((char*)buffer, "Hello from the channel!");
            return strlen((char*)buffer);
        }
    } m_read_buffer;
};

class TcpClientModule {
public:
    // 模拟 TcpClientModule 类，包含设置回调函数
    void set_read_callback(std::function<void(void*)> cb) {
        readCallback_ = cb;
    }
    
    void execute_callback(void* arg) {
        if (readCallback_) {
            readCallback_(arg);  // 执行回调
        }
    }

private:
    std::function<void(void*)> readCallback_;  // 用于保存回调函数
};

class TcpModule {
public:
    TcpModule(const std::string& ip, int port) {
        // 初始化客户端
        client = new TcpClientModule();
    }

    void set_conn_read_back(std::function<void(void*)> cb) {
        client->set_read_callback(cb);
    }

    void simulate_connection(void* arg) {
        // 模拟调用回调函数
        client->execute_callback(arg);
    }

private:
    TcpClientModule* client;  // 模拟的客户端对象
};

// 回调函数
void recv_tcp_msg(void* arg) {
    Channel* channel = static_cast<Channel*>(arg);
    char buf[1024] = {0};
    int len = channel->m_read_buffer.read((uint8_t*)buf, 1024);
    printf("Received message: %s\n", buf);
}

void test_tcp() {
    TcpModule server("1", 8080);
    server.set_conn_read_back(recv_tcp_msg);  // 设置回调函数

    // 模拟一个客户端连接
    Channel channel;
    server.simulate_connection(&channel);  // 执行回调函数
}

int main() {
    test_tcp();  // 启动测试
    return 0;
}
